
<?php defined('_JEXEC') or die('Restricted access'); ?>
<script language="javascript" type="text/javascript">
	var fieldMsg = {
		fid: "请选择农场",
		pid: "请选择植物",
		uid: "请选择计录人员",
		pre_sowing_date: "请指定计划播种日",
		pre_production:"请指定计划产量"
	};
	var _root = '<?php echo JURI::root() ?>';

	function copyDate(id) {
		jQuery("#"+id+"_table").append("<tr><td>" + jQuery("#"+id).find("td").eq(0).html() + '</td><td> <a href="###" class="left" onclick="cancelDate(this);return false;" title="删除添加"><img src="' + _root + 'images/delete.png" /></a></td>' +"</tr>");
    
	}

	function cancelDate(obj) {
		obj.parentNode.parentNode.remove();    
	}
	/*
	 *  此函数可创象一个dom对象， 以及对象相应的属性
	 *  @a tag, b hash参数 例如：
	 *  setNode("div",{
       className:"hello", 
       id:"good", 
       innerHTML:"123"
 });
	 * 
	 * */

	function setNode(a,b) {
		var obj = document.createElement(a);
		if(b) {
			for(o in b) {
				if(o=="innerHTML" || o=="id" || o=="className" || o=="name") {
					obj[o] = b[o];
				} else {
					obj.setAttribute(o, b[o]);
				}
			}
		}
		return obj;
	}

	function cancelSelect(obj) {
		jQuery(obj).parent().parent().parent().parent().parent().remove();
	}

	function cancelDate(obj) {
		jQuery(obj).parent().parent().remove();
	}
 
 
	function multiAdd(prefix) {
		this.prefix = prefix;
		this.wrap = jQuery("#"+prefix+"-wrap");
		this.selectTemp = jQuery("#"+prefix+"_select_0");
		this.dateInputTrTemp = jQuery("#"+prefix+"Date_0");
		this.AddObj = jQuery("#"+prefix+"Add");
		this.dateAddObj = jQuery("#"+prefix+"AddDate");
		this.tbodyObj = jQuery("#"+prefix+"Date_0_table");
		this.selectCount = 1,
		this.dateCount = 0,
		this.init();
	};
	multiAdd.prototype = {
		init: function() {
			this.initSelectHidden();
			this.initDateHidden();
			var _this = this;
			_this.AddObj.bind("click", function() {
				_this.selectCount ++;   
           
				jQuery("#" + _this.prefix + "SelectCount").val(_this.selectCount);
				_this.createInnerDiv();
				_this.enableDateFilter();
				_this.initDateHidden();
			});
       
		},
		initDateHidden: function() {
			var input =  setNode("input",{
				type:"hidden",
				id:  this.prefix + "DateCount_"+ (this.selectCount-1),
				value: 1
			});
			jQuery('body').append(input);
		},
		addNewDate: function(obj) {
			var table = jQuery(obj).parent().parent().parent();
			var input = this.getNewDateInput(),
            d = this.createSelectDelete(),
            i = this.createDateImg(),
            tr = setNode("tr"),
            td = setNode("td");
			jQuery(table).append(tr);
			jQuery(tr).append(td);
			jQuery(td).append(input);
			//input.id = this.prefix+"_date_"+ (this.selectCount-1) +"_" + c
			var j = "#"+this.prefix+"DateCount_"+obj.id.split("_")[1];

			jQuery(j)[0].value = parseInt(jQuery(j)[0].value) +1 ;
			input.id = this.prefix+"_date_"+ obj.id.split("_")[1] +"_" + (jQuery(j)[0].value-1);
			i.id = this.prefix+"_date_"+ obj.id.split("_")[1] +"_" + (jQuery(j)[0].value-1) +"_img";
			jQuery(d).attr("onclick", "cancelDate(this);return false;")
			jQuery(td).append(i);
			jQuery(td).append(d);
			Calendar.setup({
                inputField : input.id, // id of the input field
                ifFormat : "%Y-%m-%d", // format of the input field
                button :  i.id, // trigger for the calendar (button ID)
                align : "Tl", // alignment (defaults to "Bl")
                singleClick : true
			});
        
		},
		initSelectHidden : function() {
			var input =  setNode("input",{
				type:"hidden",
				id:  this.prefix + "SelectCount",
				value: this.selectCount
			});
			jQuery('body').append(input);
		},
		createInnerDiv: function() {
			var d= setNode("div",{
				className:this.prefix
			});
			this.wrap.append(d);
			this.wrap.append(this.getClearDiv());
			jQuery(d).append(this.getNewSelect());
			jQuery(d).append(this.getNewTable());
     
		},
   
		getNewSelect: function() {
			if(this.prefix!="userDefined") {
				var s =  setNode("select",{
					className: "manureSelect",
					innerHTML: this.selectTemp.html(),
					name: this.prefix+"["+ (this.selectCount-1) +"]"
				});
			} else {
				var s =  setNode("input",{
					type: "text",
					className:"userDefinedSelect",
					name: this.prefix+"["+ (this.selectCount-1) +"]"
				});   
			}
			return s;
		},
		getNewDateInput: function() {
			var i =  setNode("input",{
				readonly: "ture",
				type: "text",
				className: "inputbox",
				maxlength: "19",
				size: "25",
				name: this.prefix+"Date["+ (this.selectCount-1) +"][]",
				id: this.prefix+"_date_"+ (this.selectCount-1) +"_0"
			});
			return i;
		},
		getClearDiv: function() {
			var d =  setNode("div",{
				className:"clear"
			});
			return d;
		},
		getNewTable: function() {
			var t = setNode("table"),
            tb = setNode("tbody",{
                id: this.prefix+"_table_"+ (this.selectCount-1)
            }),
            tr = setNode("tr"),
            td1 = setNode("td");
            td2 = setNode("td");
			jQuery(t).append(tb);    
			jQuery(tb).append(tr);
			jQuery(tr).append(td1);
			jQuery(tr).append(td2);
			jQuery(td1).append(this.getNewDateInput());
			jQuery(td1).append(this.createDateImg());
			jQuery(td1).append(this.createAddDateButton());
			jQuery(td2).append(this.createSelectDelete());
			return t; 
		},
		createDateImg: function() {
      
			var img =  setNode("img",{
				className: "calendar",
				src: "<?php echo JURI::root() ?>templates/system/images/calendar.png",
				id: this.prefix+"_date_"+ (this.selectCount-1) +"_0_img"
			});
			return img;
		},
		createSelectDelete: function() {
			var img =  setNode("img",{
				src: "<?php echo JURI::root() ?>images/delete.png"
			}),
			a =  setNode("a",{
				className: "",
				onclick:"cancelSelect(this);return false;"
			});
			jQuery(a).append(img);
			return a;
		},
		enableDateFilter: function() {
      
			Calendar.setup({
				inputField : this.prefix+"_date_"+ (this.selectCount-1) +"_0", // id of the input field
				ifFormat : "%Y-%m-%d", // format of the input field
				button :  this.prefix+"_date_"+ (this.selectCount-1) +"_0_img", // trigger for the calendar (button ID)
				align : "Tl", // alignment (defaults to "Bl")
				singleClick : true
			});
	
		},
		enableDateFilterDate: function() {
      
			Calendar.setup({
				inputField : this.prefix+"_date_"+ (this.selectCount-1) +"_0", // id of the input field
				ifFormat : "%Y-%m-%d", // format of the input field
				button :  this.prefix+"_date_"+ (this.selectCount-1) +"_0_img", // trigger for the calendar (button ID)
				align : "Tl", // alignment (defaults to "Bl")
				singleClick : true
			});
	
		},
		createAddDateButton: function() {
			var img =  setNode("img",{
				src: "<?php echo JURI::root() ?>images/add.png"
			}),
			a =  setNode("a",{
				className: "",
				id: this.prefix+"AddDate_"+ (this.selectCount-1),
				onclick: "new_"+this.prefix+".addNewDate(this)"
			});
			jQuery(a).append(img);
          
			return a;
		}
	};

</script>
<style>
    .manureSelect, .pesticideSelect, .userDefinedSelect, .manure table, #pesticideid, .pesticide table, .left {
        float:left;
    }
    .manure {height:auto;}
    .manure table, .pesticide table,.userDefined table {

		padding-bottom: 5px;
                margin-top:-10px;
    }
    .clear {clear:both;}
    .admintable {width:500px;}

</style>
<form method="post" name="adminForm" id="adminForm">
	<fieldset>
		<div style="float: right;">
			<button class="submit" type="submit" onclick="saveForm(this.form);">
				保存</button>
			<button type="button" onclick="window.parent.document.getElementById('sbox-window').close();">
				取消</button>
		</div>
		<div class="configuration">
			新增种植计划	
		</div>
	</fieldset>
		<fieldset class="adminform">
			<legend>新增种植计划</legend>
			<table class="admintable">
				<?php JHTML::_('farm.fieldDropList'); ?>
				<?php JHTML::_('farm.plantDropList'); ?>
				<?php JHTML::_('farm.userDropList'); ?>
				<tr>
					<td width="120" class="key">
						计划播种日:
					</td>
					<td>
						<?php echo JHTML::_('calendar', '', 'pre_sowing_date', 'pre_sowing_date', '%Y-%m-%d', array('class' => 'inputbox required', 'size' => '25', 'maxlength' => '19', "readonly" => 'true')); ?>
					</td>
				</tr>
				<tr>
					<td class="key">
						计划收获日:
					</td>
					<td>
						<?php echo JHTML::_('calendar', '', 'pre_harvest', 'pre_harvest', '%Y-%m-%d', array('class' => 'inputbox required', 'size' => '25', 'maxlength' => '19', "readonly" => 'true')); ?>
					</td>
				<tr>
					<td class="key">
						计划产量:
					</td>
					<td>
						<input class="inputbox required" type="text" name="pre_production" id="pre_production" size="10" value="" />			
					</td>
				</tr>
				<tr>
					<td width="120" class="key" valign="top">
						<a id="manureAdd" href="###"  title="添加施肥方式"><img src="<?php echo JURI::root() ?>images/add.png" /></a>施肥:
					</td>
					<td>
						<div id="manure-wrap">
							<div class="manure">
								<?php JHTML::_('farm.manureDropList'); ?>

								<table>
									<tbody id="manureDate_0_table">
                                        <tr id="manureDate_0">
                                            <td>
												<?php echo JHTML::_('calendar', '', 'manureDate[0][]', 'manure_date_0_0', '%Y-%m-%d', array('class' => 'inputbox', 'size' => '25', 'maxlength' => '19', "readonly" => 'true')); ?>
												<a id="manureAddDate_0" href="###"  onclick="new_manure.addNewDate(this);" title="添加施肥方式"><img src="<?php echo JURI::root() ?>images/add.png" /></a>
                                            </td>

                                        </tr>
									</tbody>
								</table>
							</div>
                            <div style="clear:both;"></div>

						</div>
					</td>
				</tr>
				<tr>
					<td width="120" class="key" valign="top">
						<a href="###" id="pesticideAdd" title="添加农药方式"><img src="<?php echo JURI::root() ?>images/add.png" /></a>农药:
					</td>
					<td>
						<div id="pesticide-wrap">
							<div class="pesticide">
								<?php JHTML::_('farm.pesticideDropList'); ?>
								<table>
									<tbody id="pesticideDate_0_table">
                                        <tr id="pesticideDate_0">
                                            <td>
												<?php echo JHTML::_('calendar', '', 'pesticideDate[0][]', 'pesticide_date_0_0', '%Y-%m-%d', array('class' => 'inputbox', 'size' => '25', 'maxlength' => '19', "readonly" => 'true')); ?>
												<a  id="pesticideAddDate_0" href="###" onclick="new_pesticide.addNewDate(this);return false;" title="添加施肥方式"><img src="<?php echo JURI::root() ?>images/add.png" /></a>
                                            </td>
                                        </tr>

                                        </tbody>
                                    </table>
			
		     </div>
			  <div style="clear:both;"></div>
                           </div>
			</td>
		</tr>

							</div>

					</td>
				</tr>

				<tr>
					<td width="120" class="key" valign="top">
						<a href="###" id="userDefinedAdd" title="添加自定义方式"><img src="<?php echo JURI::root() ?>images/add.png" /></a>自定义:
					</td>
					<td>
						<div id="userDefined-wrap">
							<div  class="userDefined">
								<input type="text" name="userDefined[0]" class="userDefinedSelect" />
                                <table>
									<tbody id="userDefinedDate_0_table">
										<tr id="userDefinedDate_0">
                                            <td>
												<?php echo JHTML::_('calendar', '', 'userDefinedDate[0][]', 'userDefined_date_0', '%Y-%m-%d', array('class' => 'inputbox', 'size' => '25', 'maxlength' => '19', "readonly" => 'true')); ?>
												<a  id="userDefinedAddDate_0" href="###" onclick="new_userDefined.addNewDate(this);return false;" title="添加施肥方式"><img src="<?php echo JURI::root() ?>images/add.png" /></a>
                                            </td>
                                        </tr>
									</tbody>
                                </table>

							</div>
                            </div>
					</td>
				</tr>
			</table>
		</fieldset>
	</div>
	<div class="clr"></div>
	<input type="hidden" name="task" value="savePlantPlan" />
	<input type="hidden" name="controller" value="<?php echo JRequest::getWord('controller', 'farms'); ?>" />
	<input type="hidden" name="option" value="com_farm" />
	<input type="hidden" name="id" value="" />
	<?php echo JHTML::_('form.token'); ?>

	<script type="text/javascript">
		new_manure = new multiAdd("manure");
		new_pesticide = new multiAdd("pesticide");
		new_userDefined = new multiAdd("userDefined");
	</script>

</form>